package com.foftact.cave.service.impl;

import com.foftact.cave.Cave;
import com.foftact.cave.mapper.CaveMapper;
import com.foftact.cave.service.CaveService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 持仓与跟踪指数服务实现类
 */
@Service
public class CaveServiceImpl implements CaveService {

    private static final Logger logger = LoggerFactory.getLogger(CaveServiceImpl.class);

    @Autowired
    private CaveMapper caveMapper;

    @Override
    public boolean saveCave(Cave cave) {
        if (cave == null) {
            logger.error("保存持仓数据时，数据对象为空");
            return false;
        }

        try {
            // 检查是否已存在该代码的数据
            Cave existing = caveMapper.selectByCode(cave.getCode());
            if (existing != null) {
                // 如果存在则更新
                int result = caveMapper.updateByCode(cave);
                logger.info("更新持仓数据成功，代码：{}", cave.getCode());
                return result > 0;
            } else {
                // 如果不存在则插入
                int result = caveMapper.insert(cave);
                logger.info("新增持仓数据成功，代码：{}", cave.getCode());
                return result > 0;
            }
        } catch (Exception e) {
            logger.error("保存持仓数据失败，代码：{}", cave.getCode(), e);
            return false;
        }
    }

    @Override
    public boolean deleteCaveByCode(String code) {
        if (code == null || code.trim().isEmpty()) {
            logger.error("删除持仓数据时，代码为空");
            return false;
        }

        try {
            int result = caveMapper.deleteByCode(code);
            logger.info("删除持仓数据成功，代码：{}", code);
            return result > 0;
        } catch (Exception e) {
            logger.error("删除持仓数据失败，代码：{}", code, e);
            return false;
        }
    }

    @Override
    public boolean deleteCaveById(Long id) {
        if (id == null) {
            logger.error("删除持仓数据时，ID为空");
            return false;
        }

        try {
            int result = caveMapper.deleteById(id);
            logger.info("删除持仓数据成功，ID：{}", id);
            return result > 0;
        } catch (Exception e) {
            logger.error("删除持仓数据失败，ID：{}", id, e);
            return false;
        }
    }

    @Override
    public Cave getCaveByCode(String code) {
        if (code == null || code.trim().isEmpty()) {
            logger.error("查询持仓数据时，代码为空");
            return null;
        }

        try {
            return caveMapper.selectByCode(code);
        } catch (Exception e) {
            logger.error("查询持仓数据失败，代码：{}", code, e);
            return null;
        }
    }

    @Override
    public Cave getCaveById(Long id) {
        if (id == null) {
            logger.error("查询持仓数据时，ID为空");
            return null;
        }

        try {
            return caveMapper.selectById(id);
        } catch (Exception e) {
            logger.error("查询持仓数据失败，ID：{}", id, e);
            return null;
        }
    }

    @Override
    public List<Cave> getCaveListByRefcode(String refcode) {
        if (refcode == null || refcode.trim().isEmpty()) {
            logger.error("查询持仓数据时，跟踪指数代码为空");
            return null;
        }

        try {
            return caveMapper.selectByRefcode(refcode);
        } catch (Exception e) {
            logger.error("查询持仓数据失败，跟踪指数代码：{}", refcode, e);
            return null;
        }
    }

    @Override
    public List<Cave> getAllCaves() {
        try {
            return caveMapper.selectAll();
        } catch (Exception e) {
            logger.error("查询所有持仓数据失败", e);
            return null;
        }
    }

    @Override
    public List<Cave> searchCavesByKeyword(String keyword) {
        if (keyword == null || keyword.trim().isEmpty()) {
            logger.error("搜索持仓数据时，关键字为空");
            return null;
        }

        try {
            return caveMapper.searchByKeyword(keyword);
        } catch (Exception e) {
            logger.error("搜索持仓数据失败，关键字：{}", keyword, e);
            return null;
        }
    }
}